function facebookCollectDataEnd(data){
	if(control.debug) console.log("FACEBOOK DATA END");


	for(var i=0;i<data.length;i++){
		var friend = document.createElement("div");
		var img = document.createElement("img");
		var lab = document.createElement("label");

		friend.setAttribute("class","tela-2-friend");
		img.setAttribute("src",data[i].picture);

		$(lab).html(data[i].first_name);

		friend.appendChild(img);
		friend.appendChild(lab);

		$(".tela-2-debug-friends")[0].appendChild(friend);
	}

	$("#next2").fadeIn();
}

function updateProgress(perc){
	$(".progress.progress-striped.active div")[0].style.width = perc + "%";
}

function actorClick(imgEl){
	var actor = window.actors[parseInt(imgEl.target.getAttribute("actorId"))-1];
	control.selectActor(actor);
}

$(document).ready(function(){

	controlInit(function(){

		control = new Control();
		window.step = 0;
		window.quantSteps = $(".tela").size();
		window.borderWidth = 0;
		window.wrapperWidth = parseInt($("#wrapper").css("width"));
		
		// Largura do steps-container (slider)
		$("#content").css("width", window.quantSteps * (window.wrapperWidth + window.borderWidth) + "px");


		var car = control.getCarousel();
		car.createSlider($("#actorsBeforeFlag")[0],actorClick);
		car.render();

		$("button.next").click(function(e){
			control.buttonClick(e);
		});	

		$("button.week.mostrar").click(function(){
			control.showMovies();
		});			

		$("button.restart").click(function(){
			location.assign("");
		});			

		$(".ator-tool-selection input").click(function(e){
			car.updateSlider(e.target.value,e.target.checked);
			car.createSlider($("#actorsBeforeFlag")[0],actorClick);
			car.render();
		});			

		$("#facebook-connect").mouseover(function(){
			$("#span-not-nice").fadeOut(function(){
				$("#span-acao").fadeOut(function(){
					$("#span-facebook").fadeIn();						
				});
				
			});
		});					

		$("#facebook-connect").click(function(){			
			//$(".tela-2-debug-info").fadeIn();
			//facebookInit(facebookCollectDataEnd,updateProgress);	

			facebookLogin(function(answer){
				if(answer==true){
					$(".tela-2-debug-info").fadeIn();
					facebookInit(facebookCollectDataEnd,updateProgress);	

					$("#facebook-connect").fadeOut();
					$("#no-facebook").fadeOut();
				}
			})			
		});

		$("#no-facebook").click(function(){
			var data = [];
			data.push({"name":"Daniel Antunes","first_name":"Daniel","picture":"https://graph.facebook.com/daniel.m.antunes/picture?type=large","gender":"male","locale":"pt_br"});
			data.push({"name":"Luan Garrido","first_name":"Luan","picture":"https://graph.facebook.com/luan.garrido/picture?type=large","gender":"male","locale":"pt_br"});
			data.push({"name":"André Albuquerque","first_name":"André","picture":"https://graph.facebook.com/andre.albuquerque.alfa/picture?type=large","gender":"male","locale":"pt_br"});
			data.push({"name":"Ximene Antunes","first_name":"Ximene","picture":"https://graph.facebook.com/ximene.antunes/picture?type=large","gender":"female","locale":"pt_br"});
			data.push({"name":"Silvia Sailon Bonazza","first_name":"Sailon","picture":"https://graph.facebook.com/silviasailon.bonazza/picture?type=large","gender":"female","locale":"it"});	
			
			$(".tela-2-debug-message")[0].style.display = "none";
			$(".progress")[0].style.display = "none";
			$(".tela-2-debug-info").fadeIn();
			$(".tela-2-controls").fadeOut();

			window.comments = renderComments(data);
			facebookCollectDataEnd(data);
		});

		$("#no-facebook").mouseover(function(){
			$("#span-facebook").fadeOut(function(){
				$("#span-acao").fadeOut(function(){
					$("#span-not-nice").fadeIn();	
				});				
			});
		});				


	});
	
});
